package com.offdissys.common.db.mybatisplus;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author ZhangHaiWen
 * @title: CommMetaObjectHandler
 * @projectName mybatisplustest
 * @description: 自动填充的配置
 * @date 2022/2/1714:55
 */
@Component
public class CommMetaObjectHandler implements MetaObjectHandler {

    /**
     * 插入时字段自动更新
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createDate = this.getFieldValByName("createDate", metaObject);
        if(ObjectUtil.isNull(createDate)){
            this.setFieldValByName("createDate", new Date(), metaObject);
        }
    }

    /**
     * 更新时字段自动更新
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateDate = this.getFieldValByName("updateDate", metaObject);
        if(ObjectUtil.isNull(updateDate)){
            this.setFieldValByName("updateDate", new Date(), metaObject);
        }
    }
}
